import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_assets/assets.gen.dart';
import 'package:media_kit/media_kit.dart';
import 'package:qh_music/src/shared/mixins/l10n.dart';
import 'package:qh_music/src/widgets/audio/audio_controller.dart';

class AudioPlayerWidget extends StatelessWidget {
  const AudioPlayerWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final player = Player();
    final audioDevice = AudioDevice.auto();
    player.setAudioDevice(audioDevice);
    final playList = Playlist(
      Assets.music.values.map((item) {
        return Media("asset://$item");
      }).toList(),
    );
    player.open(playList).then((value) {
      player.play();
    });
    return Scaffold(
      body: Column(
        children: [
          IconButton(
            onPressed: () async {
              final result = await FilePicker.platform.pickFiles(
                dialogTitle: context.l10n.commonChooseFile,
                type: FileType.custom,
                allowedExtensions: ["mp3", "flac", "wav"],
                allowMultiple: true,
              );
              if (result != null) {
                player.open(
                  Playlist(
                    result.files.map((item) => Media(item.path ?? '')).toList(),
                  ),
                );
              }
            },
            icon: const Icon(Icons.chair_outlined),
          ),
          AudioControllerWidget(player: player)
        ],
      ),
    );
  }
}